using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ConvertSourceToHtml.Conversion.HtmlCodeElements
{

    /// <summary>
    /// Represents a line of content in an HTML file.
    /// </summary>
    public sealed class HtmlLine
    {
        #region Fields

        private List<HtmlBlock> _blockList;

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlLine"/> class.
        /// </summary>
        public HtmlLine()
        {
            _blockList = new List<HtmlBlock>();
        }

        #endregion // Constructors

        #region Properties

        /// <summary>
        /// Gets whether the line has any content.
        /// </summary>
        /// <value>
        /// <see langword="true"/> if the line has any content, <see langword="false"/> otherwise.
        /// </value>
        /// <remarks>
        /// <para>
        /// A line has content if any of the blocks that make up the line have content.
        /// </para>
        /// </remarks>
        public bool HasContent
        {
            get
            {
                foreach (var block in _blockList)
                {
                    if (block.HasContent)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// Gets the number of leading spaces for the line.
        /// </summary>
        /// <value>
        /// The number of leading spaces for the line.
        /// </value>
        /// <remarks>
        /// <para>
        /// A line's leading spaces are the leading spaces for all blocks that make up the line, up 
        /// to and including the first block that has content.  For example, if a line is made up 
        /// of four blocks, and if the first two blocks don't have content, the line's leading 
        /// spaces will be the leading spaces from the first two blocks without content plus the 
        /// leading spaces from the third block with content.
        /// </para>
        /// </remarks>
        public int LeadingSpaces
        {
            get
            {
                int leadingSpaces;

                leadingSpaces = 0;
                foreach (var block in _blockList)
                {
                    leadingSpaces += block.LeadingSpaces;
                    if (block.HasContent)
                    {
                        break;
                    }
                }
                return leadingSpaces;
            }
        }

        /// <summary>
        /// Gets the blocks that make up the line.
        /// </summary>
        public ReadOnlyCollection<HtmlBlock> Blocks
        {
            get { return new ReadOnlyCollection<HtmlBlock>(_blockList); }
        }

        #endregion // Properties

        #region Methods

        /// <summary>
        /// Adds a block to the line.
        /// </summary>
        /// <param name="block">The block to add to the line.</param>
        public void AddBlock(HtmlBlock block)
        {
            Safety.AssertArgumentNotNull("block", block);

            _blockList.Add(block);
        }

        #endregion // Methods
    }

}
